/*
 * @Author: your name
 * @Date: 2020-09-21 08:05:06
 * @LastEditTime: 2021-02-02 16:39:15
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \longh_web\src\store\index.js
 */
import Vue from 'vue'
import Vuex from 'vuex'
import { GetUserInfo, getAccessToken } from "@/apis/user";  // Login

import { listSysMsg } from '@/apis/system/msg'
import { setToken, getToken, getIsSign, removeToken, removeCookies, setIsSign, getCookiesUserInfo } from "@/utils/auth"; // get token from cookie
Vue.use(Vuex)
import router from '../router'


export default new Vuex.Store({
  state: {
    UserInfo: {},
    TokenValue: getToken(),
    issign: getIsSign(),
    noReadMsgCount: 0,
  },
  mutations: {
    VuexSetTokenValue (state, value) {
      state.TokenValue = value
    },
    VuexGetUserInfo (state, value) {
      state.UserInfo = value
    },
    setIsSign (state, value) {
      state.issign = value
      setIsSign(true);
    },
    setNoReadMsgCount (state, value) {
      state.noReadMsgCount = value
    }
  },
  actions: {
    getNoReadMsgCount ({ commit }) {
      let params = {
        pageSize: 1000,
        PageNum: 1,
        status: 0,
        userId: JSON.parse(getCookiesUserInfo()).userId,
      };
      listSysMsg(params).then(res => {
        console.log('getNoReadMsg', res);
        commit('setNoReadMsgCount', res.total)
      })

    },

    GetUserInfo () {
      GetUserInfo().then(res => {
        if (res.code == 200) {
          localStorage.setItem("LH_PC_BaseInfo", JSON.stringify(res.data));
        }
      }).catch(error => { })
    },
    getAccessToken ({ commit }, value) {
      getAccessToken(value).then(res => {
        if (res.access_token) {
          // debugger
          localStorage.setItem('LH_WEB_TOKEN', res.access_token)
          setToken(res.access_token)
          commit('VuexSetTokenValue', res.access_token)
          GetUserInfo().then(res => {
            if (res.code == 200) {
              commit('VuexGetUserInfo', res.data)
              localStorage.setItem("LH_PC_BaseInfo", JSON.stringify(res.data));
            }
          })
          // gobler_access_token = res.access_token
          // window.location.reload()
        }
        // else {
        // }
      }).catch(error => {

      })
      // let token1='43c712e6-11d3-4adf-9fe3-f180dc239b35'
      // getAccessToken(value).then(res => {
      //   if (token1) {
      //     localStorage.setItem('LH_WEB_TOKEN',token1)
      //     setToken(token1)
      //     GetUserInfo().then(res=>{
      //       if (res.code == 200) {
      //         commit('VuexGetUserInfo',res.data)
      //         localStorage.setItem("LH_PC_BaseInfo", JSON.stringify(res.data));
      //       }
      //     })
      //     // gobler_access_token = res.access_token
      //     // window.location.reload()
      //   } else {
      //   }
      // }).catch(error => {

      // })

    },
    RemoveLocalHost () {
      removeToken();
      removeCookies();
      localStorage.removeItem("LH_WEB_TOKEN");
      router.push("/");
    },
  },
  modules: {
  }
})
